package com.hzjy.api.controller;

import com.hzjy.api.beans.HzjyUserPayRecord;
import com.hzjy.api.service.BqTokenService;
import com.hzjy.api.service.impl.HzjyUserPayRecordServiceImpl;
import com.hzjy.api.vo.BqLoginUser;
import com.hzjy.common.constant.HttpStatus;
import com.hzjy.common.core.controller.BaseController;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.core.page.TableDataInfo;
import com.hzjy.common.utils.PageData;
import com.hzjy.common.utils.ServletUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户支付记录（我的发票列）Controller
 *
 * @author hzjy
 * @date 2020-08-26
 */
@RestController
@RequestMapping("/api/payRecord")
public class HzjyUserPayRecordController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(HzjyUserPayRecordController.class);
    @Autowired
    private HzjyUserPayRecordServiceImpl hzjyUserPayRecordService;
    @Autowired
    private BqTokenService bqTokenService;

    /**
     * 查询用户支付记录（我的发票列）列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody HzjyUserPayRecord hzjyUserPayRecord) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("未登录");
            return rspData;
        }
        hzjyUserPayRecord.setUserId(loginUser.getBqUser().getUserId().toString());
        startPage();
        List<HzjyUserPayRecord> list = hzjyUserPayRecordService.selectHzjyUserPayRecordList(hzjyUserPayRecord);
        return getDataTable(list);
    }

    /**
     * 新增用户支付记录（我的发票列）
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody HzjyUserPayRecord hzjyUserPayRecord) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401, "未登录");
        }
        HzjyUserPayRecord oldHzjyUserPayRecord = hzjyUserPayRecordService.selectHzjyUserPayRecordById(hzjyUserPayRecord.getId());
        if (hzjyUserPayRecord.getStatus()!=null && oldHzjyUserPayRecord.getStatus().equals(2)){
            return AjaxResult.error(500, "您已经申请过了");
        }
        hzjyUserPayRecord.setUpdateTime(new Date());
        return toAjax(hzjyUserPayRecordService.updateHzjyUserPayRecord(hzjyUserPayRecord));
    }

    /**
     * 删除用户支付记录（我的发票列）
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody HzjyUserPayRecord hzjyUserPayRecord){
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401, "未登录");
        }
        hzjyUserPayRecordService.deleteHzjyUserPayRecordById(hzjyUserPayRecord.getId());
        return AjaxResult.success();
    }

    /**
     * 申请发表数据
     */
    @PostMapping("/userPayRecordAudit")
    public AjaxResult userPayRecordAudit(@RequestBody HzjyUserPayRecord hzjyUserPayRecord){
        HzjyUserPayRecord userPayRecord = hzjyUserPayRecordService.selectUserPayRecord(hzjyUserPayRecord.getId());
        return AjaxResult.success(200,"成功",userPayRecord);
    }


    /**
     * 查询用户支付记录（我的发票列）列表
     */
    @PostMapping("/wxList")
    public AjaxResult wxList(@RequestBody PageData pageData) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401,"未登录");
        }
        pageData.put("userId",loginUser.getBqUser().getUserId().toString());
        //hzjyUserPayRecord.setUserId(loginUser.getBqUser().getUserId().toString());
        if(pageData.isEmpty()){
            pageData.put("currentPage",0);
            pageData.put("size",10);
        }else{
            pageData.put("currentPage",(Integer.parseInt(pageData.get("currentPage").toString())-1) * Integer.parseInt(pageData.get("size").toString()));
        }
        List<PageData> pageDataList = hzjyUserPayRecordService.queryHzjyUserPayRecordList(pageData);
        PageData count = hzjyUserPayRecordService.getUserPayRecordNum(pageData);
        Map map = new HashMap();
        map.put("userPayRecordList", pageDataList);
        map.put("totalResult", count.get("totalResult"));
        return AjaxResult.success(200,"查询成功",map);
    }

}
